<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 ref引用对象-->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        class="login_form"
        :rules="loginFormRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-lock_fill"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重制</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'login',
  data() {
    return {
      // 登录表单绑定的数据
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度为3字符到10字符之间',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 5,
            max: 15,
            message: '长度为5字符到15字符之间',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    resetLoginForm() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields()
    },
    // validate是否符合表单验证规则  async await返回具体的对象
    login() {
      // 目前数据库接口不可用，当可用时用下面的代码 还有路由守卫记得打开！！！！
      // this.$router.push('/home')

      // 1.登录成功保存token到sessionStorage中
      // 1.1API接口 1.2token只在网站打开期间有效
      // 2.通过编程式导航跳转页面

       this.$refs.loginFormRef.validate(async valid => {
          if (!valid) return;
           // 发起登录请求  解构data为res,方便后面读取statusz的值
           const { data: res } = await this.$http.post('login', this.loginForm);
           if(res.meta.status!==200)return this.$message.error('登录失败')
         this.$message.success('登录成功')
         // 登录成功后保存token 在sessionStorage中,不关闭页面前都能在登录里面
         window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home")
      });
    },
  },
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: rgb(38, 74, 107);
  height: 100%;
  /* display: flex; */
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  /* margin: auto; */
  /* 居中布局 */
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btn {
  display: flex;
  justify-content: flex-end;
}
</style>
